<script lang="ts" setup>
import {ref, reactive} from 'vue'


// deepClone------------------------------------------------------------------------
let obj = {
  name: 'web',
  age: 10,
  info: {
    es5: ['1', '2'],
    es6: {
      p: 1
    }
  }
}

function deepClone  (origin: any, hashMap = new WeakMap()) {
  if (origin === null || typeof origin !== 'object') {
    return origin
  }
  if (origin instanceof Date) {
    return new Date(origin)
  }
  if (origin instanceof RegExp) {
    return new RegExp(origin)
  }

  const hashKey = hashMap.get(origin)
  if (hashKey) {
    return hashKey
  }

  //  {} []
  const target = new origin.constructor()
  hashMap.set(origin, target)
  for (let k in origin) {
    if (origin.hasOwnProperty(k)) {
      target[k] = deepClone(origin[k], hashMap)
    }
  }
  return target
}

const newobj = deepClone(obj)
console.log(newobj, obj)

let test1 = {}
let test2 = {}
test2.test1 = test1
test1.test2 = test2
console.log(deepClone(test2))
// WeakMap //  键名 -> 对象    弱引用(不需要手动置空就会被垃圾回收)--键名
// Map     //  键名 -> 任意类型
// {}      //  键名 -> 字符串

const map = new WeakMap()
// map.set(fn, handleFn)  
// map.get(fn)  // fn清除掉之后handleFn也会被清除掉 





// promise-----------------------------------------------------------------
// pending -> resolve  pending -> reject







let scope = [-Math.pow(2, 31), Math.pow(2, 31) - 1]

console.log(scope, '整数数字范围')


  const list = [1, 2, 3]
  const square = (num: number) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(num * num)
      }, 1000)
    })
  }
  async function test() {
    // 修改这里的代码
    // list.forEach(async x => {
    //   const res = await square(x)
    //   console.log(res)
    // })
    for (let v of list) {
      const res = await square(v)
      console.log(res)
    }
  }
  test()

// --------------------------------------------------------------------

let sum = ([1, 2, 3]).reduce((acc, cur) => acc + cur)
console.log(sum, '和')
console.log(Math.max(1, 2, 3), '最大值')





function debounce(fn: any,delay: number){
  let timer: any = null
  return function () {
    clearTimeout(timer)
    timer = setTimeout(() => {
      fn()
    }, delay)
  }
}










</script>
<template>
  <div >

  </div>
</template>
<style lang="scss" scoped>
div:after{
  content: " ";
  position: absolute;
  width: 0px;
  height: 0px;
  border-right: 100px solid transparent;
  border-top: 100px solid transparent;
  border-left: 100px solid #f00;
  border-bottom: 100px solid transparent;
}

</style>